/**
 * Project Name:YCApiServer-admin
 * File Name:LyricsTemplateService.java
 * Package Name:com.yc.music.admin.service
 * Date:2016年6月2日下午1:39:02
 * Copyright (c) 2016 
 *
 */
package com.yc.music.service.service21;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.yc.music.common.context.YcContext;
import com.yc.music.common.util.BindingResultUtil;
import com.yc.music.context.YtConstant;
import com.yc.music.mapper.dao.DraftMapper;
import com.yc.music.model.draft.Draft;


/**
 * ClassName: DraftService <br/>
 * Function: 草稿. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2016年8月11日 下午1:39:02 <br/>
 *
 * @author qinhaiguan
 * @version 
 */
@Service
public class DraftService {

	@Autowired
	private DraftMapper draftMapper;
	
	public Map<String,Object> draftList(Integer page,Integer uid,Map<String,Object> result) throws Exception{
    	int rows=10;
		page=page==0?page=1:page;
		int start=(page-1)*rows;
		List<Draft> list = draftMapper.getDraftList(uid, start, rows);
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, list);
        return result;
    }
	
	
	public Map<String,Object>  saveDraft(Draft draft,Map<String,Object> result,BindingResult binding) throws Exception{
		String valiterMsg = BindingResultUtil.getBindErrorMsg(binding);
		if (StringUtils.isNotBlank(valiterMsg)) {
			result.put(YcContext.CODE, 400);
			result.put(YcContext.MESSAGE, valiterMsg);
			result.put(YcContext.DATA, YcContext.EMPTY_STRING);
			return result;
		}else{
			draftMapper.saveDraft(draft);
			result.put(YcContext.CODE, 200);
			result.put(YcContext.MESSAGE, YtConstant.SYS_SUCCSS_MSG);
			result.put(YcContext.DATA, YcContext.EMPTY_STRING);
	        return result;
		}
		
	}
	
	public Map<String,Object>  updateDraft(Draft draft,Map<String,Object> result) throws Exception{
		
		draftMapper.updateDraft(draft);
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, YtConstant.SYS_SUCCSS_MSG);
        return result;
	}
	
	public Map<String,Object> deleteDraftById(int id,Map<String,Object> result) throws Exception{
		draftMapper.deleteDraftById(id);
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YtConstant.SYS_SUCCSS_MSG);
		result.put(YcContext.DATA, YcContext.EMPTY_STRING);
        return result;
	}
	
}
